package com.gxa.weixing.pc.service.impl;

import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.ExchangeRecord;
import com.gxa.weixing.core.pojo.ExchangeRecordVO;
import com.gxa.weixing.core.service.impl.BaseServiceImpl;
import com.gxa.weixing.pc.mapper.ExchangeRecordMapper;
import com.gxa.weixing.pc.service.ExchangeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ExchangeRecordServiceImpl extends BaseServiceImpl<ExchangeRecord> implements ExchangeRecordService {

    @Autowired
    private ExchangeRecordMapper exchangeRecordMapper = null;

    @Override
    public List<ExchangeRecordVO> getByPage(Map<String, Object> map) throws BusinessException {
        Integer page = null;
        Integer limit = null;
        if (map.get("page") == null) {
            page = 1;
        } else {
            page = (Integer)map.get("page");
        }
        if (map.get("size") == null) {
            limit = 10;
        } else {
            limit = (Integer)map.get("size");
        }
        Integer offset = (page - 1) * limit;
        map.put("offset",offset);
        map.put("limit",limit);
        List<ExchangeRecordVO> recordByPage = exchangeRecordMapper.getRecordByPage(map);
        if (recordByPage == null || recordByPage.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_DATE);
        }
        return recordByPage;
    }

    @Override
    public Integer getByPageCount(Map<String, Object> map) {
        map.remove("offset");
        map.remove("limit");
        return exchangeRecordMapper.getRecordByPage(map).size();
    }
}
